package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.ItemPlanProp;
import com.mxpio.erp.common.technology.entity.ItemPropKey;
import com.mxpio.erp.common.technology.service.ItemPlanPropService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "ItemPlanPropController", description = "物料计划属性")
@RestController
@RequestMapping("/erp/common/item/prop/plan/")
public class ItemPlanPropController {
	
	@Autowired
	private ItemPlanPropService itemPlanPropSerivce;

	@GetMapping("list")
	@Operation(summary = "物料计划属性列表", description = "获取物料计划属性列表", method = "GET")
	public Result<List<ItemPlanProp>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ItemPlanProp> itemPlanProps = itemPlanPropSerivce.list(ItemPlanProp.class, criteria);
		return Result.OK(itemPlanProps);
	}
	
	@GetMapping("page")
	@Operation(summary = "物料计划属性列表", description = "获取物料计划属性列表", method = "GET")
	public Result<Page<ItemPlanProp>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ItemPlanProp> itemPlanProps = itemPlanPropSerivce.listPage(ItemPlanProp.class, page, criteria);
		return Result.OK(itemPlanProps);
	}
	
	@GetMapping("list/{propType}/{code}")
	@Operation(summary = "根据type，code获取物料计划属性", description = "根据type，code获取物料计划属性", method = "GET")
	public Result<ItemPlanProp> getById(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		ItemPlanProp itemPlanProp = itemPlanPropSerivce.getById(ItemPlanProp.class, key);
		return Result.OK(itemPlanProp);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增物料计划属性", description = "新增物料计划属性", method = "POST")
	public Result<ItemPlanProp> add(@RequestBody ItemPlanProp itemPlanProp) {
		itemPlanPropSerivce.save(itemPlanProp);
		return Result.OK(itemPlanProp);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑物料计划属性", description = "编辑物料计划属性（全量）", method = "PUT")
	public Result<ItemPlanProp> edit(@RequestBody ItemPlanProp itemPlanProp) {
		itemPlanPropSerivce.update(itemPlanProp);
		return Result.OK(itemPlanProp);
	}
	
	@DeleteMapping("remove/{propType}/{code}")
	@Operation(summary = "删除物料计划属性", description = "删除物料计划属性", method = "DELETE")
	public Result<ItemPlanProp> remove(@PathVariable(name = "propType", required = true) String propType,
			@PathVariable(name = "code", required = true) String code) {
		ItemPropKey key = new ItemPropKey();
		key.setPropType(propType);
		key.setCode(code);
		itemPlanPropSerivce.delete(ItemPlanProp.class, key);
		return Result.OK();
	}
	
}
